﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyServer.Common;
using System.Net;
using System.Net.Sockets;
using FrameWork.Setting;

namespace MyServer.TcpIP
{
    /// <summary>
    /// 表示一个TcpIp的数据读取器
    /// </summary>
    /// <remarks>
    /// TcpIp数据读取器配套TcpIp数据发送器使用，使用其它发送器可能会出现意想不到的问题
    /// </remarks>
    [SettingParentDefine]
    public class TcpIpDataReader : DataReaderBase
    {
        private object lockObj = new object();
        private object lockObj2 = new object();

        protected override System.IO.Stream GetStream(object Tag)
        {
            lock (lockObj)
            {
                lock (Tag)
                {
                    TcpClient c = (TcpClient)Tag;
                    return c.GetStream();
                }
            }
        }

        public TcpIpDataReader(TcpIpCenter Center, DataCompressorBase Compressor=null) : base(Center, Compressor) { }


        protected override int GetDataAvilable(object Tag)
        {
            lock (lockObj2)
            {
                lock (Tag)
                {
                    return ((TcpClient)Tag).Available;
                }
            }
        }

        protected override bool CheckClient(object Left, object Right)
        {
            return Left == Right;
        }
    }
}
